// 防抖: n 秒后在执行该事件，若在 n 秒内被重复触发，则重新计时
// 防抖：就是王者荣耀里的回城，一段时间内被打断，就又要重新计时。

function debounce(fn, delay) {
    let timer;
    return function () {
        let _this = this;
        let args = arguments;
        if (timer) clearTimeout(timer)//清除定时器
        timer = setTimeout(function () {
            fn.apply(_this, args)
        }, delay)
    }
}


function debounce(fun, delay, immediate) {
    let timer;

    return function () {
        let context = this;
        let args = arguments;

        if (timer) clearTimeout(timer); //清除定时器，重新计时

        if (immediate) {
            let callNow = !timer;
            timer = setTimeout(function () {
                timer = null;
            }, delay);
            if (callNow) fun.apply(context, args);
        } else {
            timer = setTimeout(function () {
                fun.apply(context, args);
            }, delay);
        }
    };
}